package cn.edu.scau.cmi.huangxiyan.client;

import java.util.*;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.event.ActionEvent;
import javafx.collections.*;
import javafx.scene.control.cell.*;

import cn.edu.scau.cmi.huangxiyan.hibernate.domain.*;
import cn.edu.scau.cmi.huangxiyan.hibernate.dao.*;

public class HibernateClient extends Application {
	@SuppressWarnings("rawtypes")
	private TableView studentTable, teacherTable;
	private TextField studentName, teacherName;
	private TextField studentNewName, teacherNewName;
	@SuppressWarnings("rawtypes")
	private ComboBox studentSelect, teacherSelect;
	private Button add, update, delete;
	private StudentDAO sdao = new StudentDAO();
	private TeacherDAO tdao = new TeacherDAO();
	private TextArea status;

	public static void main(String[] args) {
		Application.launch(args);
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public void start(Stage primaryStage) {
		primaryStage.setTitle("HibernateClient");

		HBox hBox = new HBox(15);
		hBox.setPadding(new Insets(0, 5, 0, 5));

		VBox vBox = new VBox(15);
		vBox.setPadding(new Insets(5, 0, 5, 0));

		Label studentNameLabel = new Label("学生姓名：");
		Label studentNewNameLabel = new Label("新学生姓名：");
		Label studentSelectLabel = new Label("选择导师：");
		studentName = new TextField();
		studentName.setPrefWidth(80);
		studentNewName = new TextField();
		studentNewName.setPrefWidth(80);
		studentSelect = new ComboBox();
		studentSelect.setPrefWidth(80);
		studentSelect.setItems(FXCollections.observableArrayList());

		HBox studentHBox = new HBox(5);
		studentHBox.setPadding(new Insets(5, 0, 5, 0));
		studentHBox.getChildren().addAll(studentNameLabel, studentName, studentNewNameLabel, studentNewName,
				studentSelectLabel, studentSelect);

		Label teacherNameLabel = new Label("老师姓名：");
		Label teacherNewNameLabel = new Label("新老师姓名：");
		Label teacherSelectLabel = new Label("选择学生：");
		teacherName = new TextField();
		teacherName.setPrefWidth(80);
		teacherNewName = new TextField();
		teacherNewName.setPrefWidth(80);
		teacherSelect = new ComboBox();
		teacherSelect.setPrefWidth(80);
		teacherSelect.setItems(FXCollections.observableArrayList());

		HBox teacherHBox = new HBox(5);
		teacherHBox.setPadding(new Insets(5, 0, 5, 0));
		teacherHBox.getChildren().addAll(teacherNameLabel, teacherName, teacherNewNameLabel, teacherNewName,
				teacherSelectLabel, teacherSelect);

		ScrollPane sp = new ScrollPane();
		sp.setPrefWidth(300);
		sp.setPrefHeight(250);
		status = new TextArea();
		status.setPrefHeight(250);
		status.setEditable(false);
		sp.setContent(status);

		add = new Button("插入");
		update = new Button("更新");
		delete = new Button("删除");

		HBox buttonHBox = new HBox(5);
		buttonHBox.setPadding(new Insets(5, 0, 5, 0));
		buttonHBox.getChildren().addAll(add, update, delete);

		vBox.getChildren().addAll(studentHBox, teacherHBox, sp, buttonHBox);

		TableColumn id, name, tutor;

		studentTable = new TableView();
		studentTable.setPrefWidth(300);
		studentTable.getColumns().add(id = new TableColumn("id"));
		studentTable.getColumns().add(name = new TableColumn("name"));
		studentTable.getColumns().add(tutor = new TableColumn("tutor"));
		ScrollPane ssp = new ScrollPane();
		ssp.setContent(studentTable);

		studentTable.setItems(FXCollections.observableArrayList());

		id.setCellValueFactory(new PropertyValueFactory<>("id"));
		name.setCellValueFactory(new PropertyValueFactory<>("name"));
		tutor.setCellValueFactory(new PropertyValueFactory<>("teacher"));

		TableColumn tid, tname;

		teacherTable = new TableView();
		teacherTable.setPrefWidth(200);
		teacherTable.getColumns().add(tid = new TableColumn("id"));
		teacherTable.getColumns().add(tname = new TableColumn("name"));
		ScrollPane tsp = new ScrollPane();
		tsp.setContent(teacherTable);

		teacherTable.setItems(FXCollections.observableArrayList());

		tid.setCellValueFactory(new PropertyValueFactory<>("id"));
		tname.setCellValueFactory(new PropertyValueFactory<>("name"));

		add.setOnAction((ActionEvent e) -> {
			String studentN = studentName.getText();
			String teacherN = teacherName.getText();

			if (studentN.compareTo("") != 0) {
				Student s = new Student();
				s.setName(studentN);

				if (sdao.save(s) == true) {
					status.appendText("新建学生 " + studentN + " 成功\n");
					studentName.setText("");
				}
			}

			if (teacherN.compareTo("") != 0) {
				Teacher t = new Teacher();
				t.setName(teacherN);

				if (tdao.save(t) == true) {
					status.appendText("新建老师 " + teacherN + " 成功\n");
					teacherName.setText("");
				}
			}

			updateTable();
		});

		delete.setOnAction((ActionEvent e) -> {
			String studentN = studentName.getText();
			String teacherN = teacherName.getText();

			if (studentN.compareTo("") != 0) {
				List tmp = sdao.findByName(studentN);

				if (tmp != null && tmp.size() != 0) {
					Student s = (Student) (tmp.toArray()[0]);

					if (sdao.delete(s) == true) {
						status.appendText("删除学生 " + studentN + " 成功\n");
						studentName.setText("");
					}
				}
			}

			if (teacherN.compareTo("") != 0) {
				List tmp = tdao.findByName(teacherN);

				if (tmp != null && tmp.size() != 0) {
					Teacher t = (Teacher) (tmp.toArray()[0]);

					if (tdao.delete(t) == true) {
						status.appendText("删除老师 " + teacherN + " 成功\n");
						teacherName.setText("");
					}
				}
			}

			updateTable();
		});

		update.setOnAction((ActionEvent e) -> {
			String studentN = studentName.getText();
			String teacherN = teacherName.getText();
			String studentNN = studentNewName.getText();
			String teacherNN = teacherNewName.getText();

			if (studentN.compareTo("") != 0) {
				List tmp = sdao.findByName(studentN);

				if (tmp != null && tmp.size() != 0) {
					Student s = (Student) (tmp.toArray()[0]);

					if (studentNN.compareTo("") != 0) {
						s.setName(studentNN);

						if (sdao.update(s) == true) {
							status.appendText("学生 " + studentN + " 姓名修改为 " + studentNN + " 成功\n");
							studentName.setText("");
							studentNewName.setText("");
						}
					}

					if (studentSelect.getValue() != null) {
						tmp = tdao.findByName(studentSelect.getValue());
						Teacher t = (Teacher) (tmp.toArray()[0]);

						if (s.getTeacher() != null && s.getTeacher().getName().compareTo(t.getName()) == 0) {
							s.setTeacher(null);
						} else {
							s.setTeacher(t);
						}

						if (sdao.update(s) == true) {
							status.appendText("学生 " + studentN + " 导师修改为 " + studentSelect.getValue() + " 成功\n");
							studentName.setText("");
						}
					}
				}
			}

			if (teacherN.compareTo("") != 0) {
				List tmp = tdao.findByName(teacherN);

				if (tmp != null && tmp.size() != 0) {
					Teacher t = (Teacher) (tmp.toArray()[0]);

					if (teacherNN.compareTo("") != 0) {
						t.setName(teacherNN);

						if (tdao.update(t) == true) {
							status.appendText("老师 " + teacherN + " 姓名修改为 " + teacherNN + " 成功\n");
							teacherName.setText("");
							teacherNewName.setText("");
						}
					}

					if (teacherSelect.getValue() != null) {
						tmp = sdao.findByName(teacherSelect.getValue());
						Student s = (Student) (tmp.toArray()[0]);

						if (s.getTeacher() != null && s.getTeacher().getName().compareTo(teacherN) == 0) {
							s.setTeacher(null);
						} else {
							s.setTeacher(t);
						}
						if (sdao.update(s) == true) {
							status.appendText("老师 " + teacherN + " 学生修改为 " + teacherSelect.getValue() + " 成功\n");
							teacherName.setText("");
						}
					}
				}
			}

			updateTable();
		});

		updateTable();

		hBox.getChildren().addAll(ssp, tsp, vBox);

		Group root = new Group();
		root.getChildren().add(hBox);
		Scene scene = new Scene(root, 1000, 420, Color.WHITE);
		primaryStage.setScene(scene);
		primaryStage.show();
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void updateTable() {
		ObservableList<StudentModel> studentData = studentTable.getItems();
		ObservableList<TeacherModel> teacherData = teacherTable.getItems();
		ObservableList<String> studentSelData = studentSelect.getItems();
		ObservableList<String> teacherSelData = teacherSelect.getItems();
		List tmp = null;

		studentData.clear();
		teacherData.clear();
		studentSelData.clear();
		teacherSelData.clear();

		tmp = sdao.findAll();
		if (tmp != null && tmp.size() != 0) {
			Object[] arr = tmp.toArray();

			for (Object i : arr) {
				Student v = (Student) i;
				Teacher t = v.getTeacher();
				studentData.add(new StudentModel(v.getId() + "", v.getName(), (t == null ? "null" : t.getId()) + ""));
				teacherSelData.add(v.getName());
			}
		}

		tmp = tdao.findAll();
		if (tmp != null && tmp.size() != 0) {
			Object[] arr = tmp.toArray();

			for (Object i : arr) {
				Teacher v = (Teacher) i;
				teacherData.add(new TeacherModel(v.getId() + "", v.getName()));
				studentSelData.add(v.getName());
			}
		}
	}
}
